package com.bst.etl.datax.web;

import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.core.page.TableDataInfo;
import com.bst.common.utils.LoginAPIUtil;
import com.bst.datatx.core.biz.model.ReturnT;
import com.bst.datatx.core.util.DateUtil;
import com.bst.etl.datax.core.cron.CronExpression;
import com.bst.etl.datax.core.util.I18nUtil;
import com.bst.etl.datax.entity.JobTemplate;
import com.bst.etl.datax.mapper.JobTemplateMapper;
import com.bst.etl.datax.service.JobTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * template controller
 *
 * @author jingwk 2019-12-22 16:13:16
 */
@Api(tags = "任务配置接口")
@Controller
@RequestMapping("/base/job/template")
public class JobTemplateController extends BaseController {

    private String prefix = "etl/job/template";

    @Resource
    private JobTemplateService jobTemplateService;
    @Autowired
    private JobTemplateMapper jobTemplateMapper;

    @GetMapping()
    public String flow(ModelMap mmap)
    {
        return prefix + "/template";
    }

    @PostMapping("/list")
    @ApiOperation("任务模板列表")
    @ResponseBody
    public TableDataInfo pageList(JobTemplate template) {
        startPage();
        List<JobTemplate> list = jobTemplateService.selectJobTemplateList(template);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ApiOperation("添加任务模板")
    @ResponseBody
    public AjaxResult add(HttpServletRequest request, JobTemplate jobTemplate) {
        jobTemplate.setUserId(LoginAPIUtil.getLoginUserId());
        ReturnT<String> r = jobTemplateService.add(jobTemplate);
        if(r.getCode()==ReturnT.SUCCESS_CODE) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(r.getMsg());
        }
    }

    /**
     * 修改基础图表配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        JobTemplate group = jobTemplateMapper.loadById(id);
        mmap.put("jobTemplate", group);
        return prefix + "/edit";
    }

    @PostMapping("/update")
    @ApiOperation("更新任务")
    @ResponseBody
    public AjaxResult update(HttpServletRequest request,JobTemplate jobTemplate) {
        jobTemplate.setUserId(LoginAPIUtil.getLoginUserId());
        ReturnT<String> r = jobTemplateService.update(jobTemplate);
        if(r.getCode()==ReturnT.SUCCESS_CODE) {
            return AjaxResult.success();
        }
        return AjaxResult.error(r.getMsg());
    }

    @PostMapping(value = "/remove")
    @ApiOperation("移除任务模板")
    @ResponseBody
    public AjaxResult remove(String ids) {
        Integer id = Integer.parseInt(ids);
        ReturnT<String> r =  jobTemplateService.remove(id);
        if(r.getCode()==ReturnT.SUCCESS_CODE) {
            return AjaxResult.success();
        }
        return AjaxResult.error(r.getMsg());
    }

    @GetMapping("/nextTriggerTime")
    @ApiOperation("获取近5次触发时间")
    @ResponseBody
    public ReturnT<List<String>> nextTriggerTime(String cron) {
        List<String> result = new ArrayList<>();
        try {
            CronExpression cronExpression = new CronExpression(cron);
            Date lastTime = new Date();
            for (int i = 0; i < 5; i++) {
                lastTime = cronExpression.getNextValidTimeAfter(lastTime);
                if (lastTime != null) {
                    result.add(DateUtil.formatDateTime(lastTime));
                } else {
                    break;
                }
            }
        } catch (ParseException e) {
            return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid"));
        }
        return new ReturnT<>(result);
    }
}
